package Homework729;
/*在文件中准备用户名和密码，user.txt
每个用户输入用户名和密码输入三次后，被锁10秒，如果再次输入三次后错误，将被冻结
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Scanner;
import java.util.stream.Collectors;

public class homework02 {
	private static Properties p = new Properties(); 


	public static void main(String[] args) throws Exception {
		Properties p=new Properties();
		//读数据
		BufferedReader br=new BufferedReader(new FileReader("user.txt"));
		ArrayList<String> list=new ArrayList<>();
		String str;
		while((str=br.readLine())!=null){
			list.add(str);
		}
		br.close();
		System.out.println(list);
		//放入map集合判断
		Map<String, String> map= list.stream()
				.collect(Collectors.toMap(
						s->s.split("=")[0],
						s->s.split("=")[1]
						));
		//输入
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入用户名：");
		String uid=sc.nextLine();
		int count=0;
		if(map.containsKey(uid)){
			loadLockState(uid);

			if (isUserLocked(uid)) {  
				System.out.println("该用户已被冻结，剩余时间为：" + getRemainingLockTime(uid) + "秒，请稍后重试");  
				return; // 退出程序  
			} 
			System.out.println("请输入密码：");
			for(int i=1;i<=3;i++){
				String password=sc.nextLine();
				count++;
				if(!password.equals(map.get(uid))){
					System.out.println("密码错误请重新输入");
				}else{
					System.out.println("用户登录成功");
					p.remove(map.get(uid));
					Writer writer = new FileWriter("prop.txt");
					p.store(writer, "保存冻结数据");
					return;
				}
			}
		}else{
			System.out.println("该用户不存在，请重新输入");
		}
		if(count==3){
			System.out.println("该用户密码已输错三次,该账户已被冻结");
			freezeUser(uid);
			Thread.sleep(10000);
		}
	}

	private static void freezeUser(String uid) throws IOException {
		if(!p.containsKey(uid)){
			p.setProperty(uid, String.valueOf(System.currentTimeMillis()));

			Writer writer = new FileWriter("prop.txt");  
			p.store(writer, "保存冻结数据");  
			writer.close(); 
		}else{
			p.setProperty(uid, "0");

			Writer writer = new FileWriter("prop.txt");  
			p.store(writer, "保存冻结数据");  
			writer.close(); 
		}
	}

	private static long getRemainingLockTime(String uid) {
		long lockTime = Long.parseLong(p.getProperty(uid));  
		return (10000 - (System.currentTimeMillis() - lockTime)) / 1000;  
	}

	private static boolean isUserLocked(String uid) {
		String lockTimeStr = p.getProperty(uid);
		if (lockTimeStr != null) { 
			if(Long.parseLong(lockTimeStr)!=0){
				long lockTime = Long.parseLong(lockTimeStr);  
				return System.currentTimeMillis() - lockTime < 10000;
			}else if(Long.parseLong(lockTimeStr)==0){
				System.out.println("该用户已被永久冻结");
				System.exit(0);
			}
		}
		return false; 
	}

	private static void loadLockState(String uid) throws IOException {
		File file = new File("prop.txt");
		
		if(!file.exists()){
			return;
		}else{
			Reader reader = new FileReader("prop.txt");  
			p.load(reader);  
			reader.close();
		}
	}

}
